package com.margo.project.cors.browser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

public class MargoBrower {
	public static void main(String[] args) {
		optionsRequest();
		formRequest();
	}
	
	/**
	 * 
	 */
	public static void optionsRequest() {
		try {
		    Socket socket = new Socket();
		    InetSocketAddress address=new InetSocketAddress("localhost", 8081);
		    socket.connect(address,10000); 
		    OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8");
		    osw.write("OPTIONS /cors/form2 HTTP/1.1\r\n");
		    osw.write("Host: localhost:8081\r\n");
		    osw.write("Connection: keep-alive\r\n");
		    osw.write("Access-Control-Request-Method: POST\r\n");
		    osw.write("Origin: http://localhost:8080\r\n");
		    osw.write("User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36\r\n");
		    osw.write("Access-Control-Request-Headers: token \r\n");
		    osw.write("Accept: */*\r\n");
		    osw.write("Referer: http://localhost:8080/cors/cors.html\r\n");
		    osw.write("Accept-Encoding: gzip, deflate, br\r\n");
		    osw.write("Accept-Language: zh-CN,zh;q=0.9\r\n");
		    //http协议必须在报文头后面再加一个换行，通知服务器发送完成，不然服务器会一直等待
		    osw.write("\r\n");
		    
		    osw.flush();
		    socket.shutdownOutput();
		    BufferedReader bufferedReader = new BufferedReader(
		            new InputStreamReader(socket.getInputStream(), "utf-8"));
		    String line = null;
		    while ((line = bufferedReader.readLine()) != null) {
		        System.out.println(line);
		    }
		    osw.close();
		    bufferedReader.close();
		    socket.close();		
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**
	 * 
	 */
	public static void formRequest() {
		try {
		    Socket socket = new Socket();
		    InetSocketAddress address=new InetSocketAddress("localhost", 8081);
		    socket.connect(address,10000); 
		    OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8");
		    osw.write("POST /cors/form HTTP/1.1\r\n");
		    osw.write("Host: localhost:8081\r\n");
		    osw.write("Connection: keep-alive\r\n");
		    osw.write("Content-Length: 17\r\n");
		    osw.write("Accept: application/json, text/javascript, */*; q=0.01\r\n");
		    osw.write("Origin: http://localhost:8080\r\n");
		    osw.write("User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36\r\n");
		    osw.write("token: U2PGBjguSuJg\r\n");
		    osw.write("Content-Type: application/x-www-form-urlencoded\r\n");
		    osw.write("Referer: http://localhost:8080/cors/cors.html\r\n");
		    osw.write("Accept-Encoding: gzip, deflate, br\r\n");
		    osw.write("Accept-Language: zh-CN,zh;q=0.9\r\n");
		    osw.write("\r\n");
		    osw.write("name=margo&age=27\r\n");
		    //http协议必须在报文头后面再加一个换行，通知服务器发送完成，不然服务器会一直等待
		    osw.write("\r\n");
		    
		    osw.flush();
		    socket.shutdownOutput();
		    BufferedReader bufferedReader = new BufferedReader(
		            new InputStreamReader(socket.getInputStream(), "utf-8"));
		    String line = null;
		    while ((line = bufferedReader.readLine()) != null) {
		        System.out.println(line);
		    }
		    osw.close();
		    bufferedReader.close();
		    socket.close();		
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
